package server;

import both.AbstractFileListener;
import both.Game;
import both.Question;
import both.StringFormatter;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ServerGameTab extends JPanel implements ActionListener{

    private JButton skipButton;
    private static JButton pauseButton;
    private JButton backButton;
    private int screenWidth = (int) Game.width;
    private int screenHeight = (int) Game.height;
    private static GameTimer gameTimer;
    private static boolean paused = false;
    private GameShowServer server;

    public ServerGameTab(GameShowServer server) {
        this.server=server;
        server.setGameTab(this);
        Question question = server.getGame().getCurrentQuestion();
        displayQuestion(question);
        JPanel buttonPanel = new JPanel();
        skipButton = new JButton(StringFormatter.formatString("SKIP",StringFormatter.QUESTION_STR));
        skipButton.addActionListener(this);
        pauseButton = new JButton(StringFormatter.formatString("PAUSE",StringFormatter.QUESTION_STR));
        pauseButton.addActionListener(this);
        backButton = new JButton(StringFormatter.formatString("BACK",StringFormatter.QUESTION_STR));
        backButton.addActionListener(this);
        gameTimer = new GameTimer();
        gameTimer.setTimerSize(100);
        if(question.getTime()!=0) {
            gameTimer.setTime(question.getTime());
        } else {
            gameTimer.setTime(Game.getDefaultTime());
        }
        gameTimer.start();
        buttonPanel.add(skipButton);
        buttonPanel.add(pauseButton);
        buttonPanel.add(backButton);
        add(gameTimer);
        add(buttonPanel);
        this.setName("server-game-tab");
    }

    public void displayQuestion(Question question) {
        JLabel questionText = new JLabel(StringFormatter.formatString(question.getQuestionText(), StringFormatter.TITLE_STR, screenWidth - 25));
        JPanel questionPanel = new JPanel();
        questionPanel.setLayout(new GridLayout(0, 1, 10, 30));
        questionPanel.add(questionText);
        add(questionPanel);
        ArrayList<String> answers = question.getAnswerChoices();
        for (int i = 0; i < answers.size(); i++) {
            JPanel panel = new JPanel();
            ////panel.setBackground(Color.black);
            JButton button = new JButton(StringFormatter.formatString("Select->", StringFormatter.ANSWER_STR));
            ////button.setActionCommand("" + i);
            JLabel label = new JLabel(StringFormatter.formatString(answers.get(i), StringFormatter.QUESTION_STR));
            ////label.setForeground(Color.white);
            //buttons.add(button);
            ////labels.add(label);
            //panel.add(button);
            panel.add(label);
            questionPanel.add(panel);
        }
    }

    public static void pause() {
        if (isPaused()) {
            pauseButton.setText(StringFormatter.formatString("PAUSE",StringFormatter.QUESTION_STR));
            gameTimer.start();
            paused = !paused;
        } else {
            pauseButton.setText(StringFormatter.formatString("UNPAUSE",StringFormatter.QUESTION_STR));
            paused = !paused;
            gameTimer.stop();
        }
    }

    public void skip() {
        boolean wasPaused=false;
        if(!paused){
            pause();
            wasPaused=true;
        }
        int n=JOptionPane.showConfirmDialog(null,StringFormatter.formatString("Are you sure?",StringFormatter.QUESTION_STR));
        if(n==JOptionPane.YES_OPTION){
            Server.changeGameTab();
        }
        if(paused && wasPaused){
            pause();
        }
    }
    
    public void go(){
        gameTimer.setTime(0);
        Server.changeGameTab();
    }

    public void back() {
        Server.changeGameTabBack();
    }

    public static boolean isPaused() {
        return paused;
    }
    
    @Override
    public void actionPerformed(ActionEvent event) {
        JButton b = (JButton) event.getSource();
        if (b == skipButton) {
            skip();
        }
        if (b == pauseButton) {
            pause();
        }
        if (b == backButton) {
            back();
        }
    }
}
